package com.dwd.order.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.SystemContext;
import com.dwd.order.service.IAdoptService;
import com.dwd.servicemanager.pojo.Product;
import com.dwd.user.pojo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/27 14:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/adoptOrder")
public class AdoptController {
    @Autowired
    private IAdoptService adoptService;

    /**
     * 保存订单
     *
     * @param map
     * @return
     */
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody Map<String, Long> map, HttpServletRequest httpServletRequest) {
        try {
            LoginInfo loginInfo = SystemContext.getLoginInfo(httpServletRequest);
            adoptService.submitOrder(map,loginInfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 根据id查产品
     * @param id
     * @return
     */
    @PostMapping("/queryProduct/{id}")
    public AjaxResult queryProduct(@PathVariable("id") String id) {
        try {
          Product product= adoptService.queryProduct(Long.valueOf(id));
            return AjaxResult.me().setResultObj(product);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
}
